/* Author: Mark Mckillion
 * Student ID: B00540772
 * Date: 07/03/2012
 * Version 1.0.*/
package assignment3;

import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class ControlPanel extends JPanel
{   /*This class will provide the ControlPanel interface and will aloow the user
     *to specifiy the incoming and outgoing threads of cash. Will also allow the
     *user to define the duration of the program.*/
    
    //Create buttons which will be used to Start/Stop the program.
    private JButton jButton1, jButton2;
    
    //Create labels to improve the layout and understandability of the program.
    private JLabel  jLabel1, jLabel10, jLabel11, jLabel12, jLabel13, jLabel14,
                    jLabel15, jLabel16, jLabel2, jLabel3, jLabel4, jLabel5,
                    jLabel6, jLabel7, jLabel8, jLabel9;
    
    //Create text fields that the user can amend to suit thier situation.
    private JTextField  jTextField1, jTextField2, jTextField3, jTextField4, 
                        jTextField5, jTextField6, jTextField7, jTextField8, 
                        jTextField9, jTextField10, jTextField11, jTextField13, 
                        jTextField14, jTextField15, jTextField16, 
                        jTextField17, jTextField18, jTextField19, jTextField20, 
                        jTextField21, jTextField22, jTextField23;
    
    //Create Threads that will execute concurrently throughout the program.
    private Thread incoming1, incoming2, outgoing1, outgoing2, outgoing3,
                          outgoing4, outgoing5, redraw;
    
    //Create Strings that will be defined by the user and used within the Threads.
    private String income1, income2, outg1, outg2, outg3, outg4, outg5;
    
    //Create Ints that will be defined by the user and used within the Threads.
    private int inTime1, inTime2, outTime1, outTime2, outTime3, outTime4, 
                outTime5, in1, in2, out1, out2, out3, out4, out5;
    
    /*Create a long that will be defined by the user and used to determaine the
     *duration of the program.*/
    private long duration;
    
    //Create a timer that will be used to control the Threads.
    private Timer myTimer;
    
    //Create a bank account that will record update and show the balance.
    private BankAccount account;
    
    //Create a DrawingPanel that will be used to show the graphical output.
    private DrawingPanel diagram;
       
    {   //Use parameters of all of the fields with information to guide the user.
        jTextField1 = new JTextField("Type");
        jTextField2 = new JTextField("Type");
        jTextField3 = new JTextField("0");
        jTextField4 = new JTextField("0");
        jTextField5 = new JTextField("0");
        jTextField6 = new JTextField("0");
        jTextField7 = new JTextField("52");
        jTextField8 = new JTextField("0");
        jTextField9 = new JTextField("0");
        jTextField10 = new JTextField("Type");
        jTextField11 = new JTextField("Type");
        jTextField13 = new JTextField("0");
        jTextField14 = new JTextField("Type");
        jTextField15 = new JTextField("0");
        jTextField16 = new JTextField("0");
        jTextField17 = new JTextField("0");
        jTextField18 = new JTextField("0");
        jTextField19 = new JTextField("0");
        jTextField20 = new JTextField("Type");
        jTextField21 = new JTextField("0");
        jTextField22 = new JTextField("0");
        jTextField23 = new JTextField("Type");
        jLabel1 = new JLabel("Income Stream 1:");
        jLabel2 = new JLabel("Income Stream 2:");
        jLabel3 = new JLabel("Type");
        jLabel4 = new JLabel("Amount (£)");
        jLabel5 = new JLabel("Frequency");
        jLabel6 = new JLabel("(Weeks)");
        jLabel7 = new JLabel("Duration (Weeks):");
        jLabel8 = new JLabel("(Weeks)");
        jLabel9 = new JLabel("Amount (£)");
        jLabel10 = new JLabel("Frequency");
        jLabel11 = new JLabel("Type");
        jLabel12 = new JLabel("Outgoing 1:");
        jLabel13 = new JLabel("Outgoing 2:");
        jLabel14 = new JLabel("Outgoing 3:");
        jLabel15 = new JLabel("Outgoing 4:");
        jLabel16 = new JLabel("Outgoing 5:");
        jButton1 = new JButton("Start");        
               
        /*Create an actionlistener that will start the program when the start
         *button is pressed.*/
        class StartListener implements ActionListener
        {   //override the actionPerformed method to listen for a user action.
            @Override
            public synchronized void actionPerformed (ActionEvent ae)
            {   /*Make the DrawingPanel within this class equal to the 
                 *DrawingPanel within ModelFrame to enusre that it will update*/               
                diagram = new DrawingPanel();
                diagram = ModelFrame.drawing;
                
                //Create a new bank account to be used.
                account = new BankAccount();
                
                /*Initialize the the duration from text field 7 to determain the
                 *length of execution.*/
                duration = Integer.parseInt(jTextField7.getText());
                
                /*Create the timer that will control the Threads and will run 
                 *for the length of duration, also will include the StopThreads
                 *method that will stop the timer once complete.*/
                myTimer = new Timer();
                myTimer.schedule(new StopThreads(), duration*1000);
                
                //Determine the name of the transaction taking place.
                income1 = jTextField1.getText();
                income2 = jTextField2.getText();
                outg1 = jTextField11.getText();
                outg2 = jTextField10.getText();
                outg3 = jTextField14.getText();
                outg4 = jTextField23.getText();
                outg5 = jTextField20.getText();
                
                //Determine the amount of the transaction taking place.
                in1 = Integer.parseInt(jTextField3.getText());
                in2 = Integer.parseInt(jTextField4.getText());
                out1 = Integer.parseInt(jTextField9.getText());
                out2 = Integer.parseInt(jTextField8.getText());
                out3 = Integer.parseInt(jTextField15.getText());
                out4 = Integer.parseInt(jTextField21.getText());
                out5 = Integer.parseInt(jTextField17.getText());
                
                //Determine the frequency of the transaction taking place.
                inTime1 = Integer.parseInt(jTextField5.getText());
                inTime2 = Integer.parseInt(jTextField6.getText());
                outTime1 = Integer.parseInt(jTextField13.getText());
                outTime2 = Integer.parseInt(jTextField16.getText());
                outTime3 = Integer.parseInt(jTextField18.getText());
                outTime4 = Integer.parseInt(jTextField19.getText());
                outTime5 = Integer.parseInt(jTextField22.getText());            
                
                //Create the Threads that will deposit and withdraw from the balance.
                incoming1 = new Thread(new Incoming(income1, inTime1*1000, in1, account));
                incoming2 = new Thread(new Incoming(income2, inTime2*1000, in2, account));
                outgoing1 = new Thread(new Outgoing(outg1, outTime1*1000, out1, account));
                outgoing2 = new Thread(new Outgoing(outg2, outTime2*1000, out2, account));
                outgoing3 = new Thread(new Outgoing(outg3, outTime3*1000, out3, account));
                outgoing4 = new Thread(new Outgoing(outg4, outTime4*1000, out4, account));
                outgoing5 = new Thread(new Outgoing(outg5, outTime5*1000, out5, account));
                
                //Create a RedrawThread that will update the GUI every second.
                redraw = new Thread(new RedrawThread(diagram, 1000, 0, account));
                
                //Start the Threads if valid information has been entered.
                if(inTime1 > 0 && in1 > 0)
                {   incoming1.start();
                }
                
                if(inTime2 > 0 && in2 > 0)
                {   incoming2.start();
                }
                
                if(outTime1 > 0 && out1 > 0)
                {   outgoing1.start();
                }
               
                if(outTime2 > 0 && out2 > 0)
                {   outgoing2.start();
                }
                
                if(outTime3 > 0 && out3 > 0)
                {   outgoing3.start();
                }
                
                if(outTime4 > 0 && out4 > 0)
                {   outgoing4.start();
                }
                
                if(outTime5 > 0 && out5 > 0)
                {   outgoing5.start();
                }
                
                //always start the redraw method.
                redraw.start();
                }
        }
        
        /*Create an actionlistener that will stop the program when the stop
         *button is pressed.*/
        class StopListener implements ActionListener
        {   //override the actionPerformed method to listen for a user action.
            @Override
            public synchronized void actionPerformed (ActionEvent ae)
            {   //Stop the Threads from running to stop the program.
                incoming1.interrupt();
                incoming2.interrupt();
                outgoing1.interrupt();
                outgoing2.interrupt();
                outgoing3.interrupt();
                outgoing4.interrupt();
                outgoing5.interrupt();
                redraw.interrupt();
                
                /*Cancel the timer to stop it from running, this also stops the 
                 *Threads assoicated with it.*/
                myTimer.cancel();
                
                /*Set the balance and duration back to 0 and repaint the graph
                 *so that it is ready to accept new details.*/
                diagram.setBalance(0);
                diagram.setDuration(0);
                diagram.repaint();
            }
        }
        
        //Add the action listeners to the buttons.
        jButton1.addActionListener(new StartListener());
        jButton2 = new JButton("Stop");
        jButton2.addActionListener(new StopListener());
        
        //SwingGUI form used for layout, code from the form shown below.
        {   
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel7))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jLabel4))
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 2, Short.MAX_VALUE))
                    .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE))
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel14)
                            .addComponent(jLabel13)
                            .addComponent(jLabel12))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField10, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(jTextField11, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(jLabel11)
                            .addComponent(jTextField14, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel16)
                            .addComponent(jLabel15))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField20, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(jTextField23, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField15, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jTextField9, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                            .addComponent(jTextField8, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField17, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                            .addComponent(jTextField21, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10)
                    .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField18, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField19, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField22, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(32, 32, 32))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel14)
                                    .addComponent(jTextField14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField18, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel15)
                                    .addComponent(jTextField23, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField21, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField17)
                                    .addComponent(jTextField20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(141, 141, 141)
                                .addComponent(jTextField22, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel10))
                        .addGap(1, 1, 1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel11)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8))
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel12)
                            .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel13)
                            .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))))
                .addGap(22, 22, 22))
        );
    }
        }
        
        /*Create an inner class that will be used to stop the Threads when the
         *Timer has ran its course.*/
        public class StopThreads extends TimerTask
        {   @Override
            public synchronized void run()
            {   //Stop the threads and the timer when the Timer has finished.
                incoming1.interrupt();
                incoming2.interrupt();
                outgoing1.interrupt();
                outgoing2.interrupt();
                outgoing3.interrupt();
                outgoing4.interrupt();
                outgoing5.interrupt();
                redraw.interrupt();
                myTimer.cancel();
            }
        }
}// End of class ControlPanel